﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fis.Win.Utils;

namespace Fis.Win.Forms
{
    public partial class RateListForm : ListForm
    {
        public RateListForm()
        {
            InitializeComponent();
            RightsName = "BD.Rate";
        }

        public override void InitializeBinding()
        {
            var d = ViewModel["viewData"] as DataTable;
            gridControl1.DataSource = d;
            UIUtils.LookUpBinding(repDefault, CacheContext.YesNoCache(), "display", "value");
        }

        protected override void NewProcess()
        {
            UIManager.ShowEdit<RateEditForm>("RateEditViewProvider");
            Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

            RefreshProcess();
        }

        protected override void EditProcess()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var data = gridView1.GetDataRow(gridView1.FocusedRowHandle);

            UIManager.ShowEdit<RateEditForm>("RateEditViewProvider"
                            , Agile.Engine.View.ViewState.Modify
                            , Agile.Engine.Utils.DataUtils.ToEditModel("viewData", data));
            Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

            RefreshProcess();
        }

        protected override void DelProcess()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var data = gridView1.GetDataRow(gridView1.FocusedRowHandle);

            if (DialogResult.Yes == DevExpress.XtraEditors.XtraMessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo))
            {
                ViewModel["SID"] = data["SID"];
                ViewModel["Action"] = "Delete";
                ViewProvider.ExecuteUpdate(ViewModel);
                Fis.Win.Utils.CacheContext.RefreshCache(ViewProvider.DataTableName);

                RefreshProcess();
            }
        }

        protected override void FindProcess(string value)
        {
            gridView1.ApplyFindFilter(value);
        }

        protected override void ExportProcess(string file)
        {
            var option = new DevExpress.XtraPrinting.XlsExportOptions();
            option.SheetName = Text;
            gridControl1.ExportToXls(file, option);
        }

        public override void ToXls(string path)
        {
            ExportProcess(path);
        }

        public override string FromXls(string path)
        {
            var error = string.Empty;
            var data = DataTransUtils.ReadSheet(path, Text, ref error);

            //读取出错
            if (data == null)
            {
                return "Excel格式错误或者Excel中的Sheet名称不正确。";
            }

            ///实体数据
            DataTable source = (gridControl1.DataSource as DataTable);
            DataTable schema = source.Copy();
            schema.Clear();

            foreach (DataRow row in data.AsEnumerable())
            {
                var newRow = schema.NewRow();
                foreach (DataColumn col in data.Columns)
                {
                    for (var i = 0; i < gridView1.VisibleColumns.Count; i++)
                    {
                        var gridCol = gridView1.VisibleColumns[i];
                        if (gridCol.FieldName.Equals("FISBASE"))
                            continue;
                        //列匹配的情况
                        if (col.ColumnName.Trim().Equals(gridCol.Caption))
                        {
                            if (gridCol.FieldName.Equals("FCODE"))
                            {
                                //编码不能重复
                                var validCode = schema.Compute("Count(FCODE)", string.Format("FCODE='{0}'", row[col.ColumnName]));
                                if (ReportUtils.NullParse(validCode) > 1)
                                {
                                    return string.Format("导入失败，原因：{0}[{1}]重复", col.ColumnName, row[col.ColumnName]);
                                }
                            }

                            newRow[gridCol.FieldName] = row[col.ColumnName];
                            break;
                        }
                    }

                }
                schema.Rows.Add(newRow);
            }

            ViewModel["viewData"] = schema;
            InitializeBinding();

            return string.Empty;
        }
    }
}
